package com.hk.service.product.impl;

import com.hk.commons.LongId;
import com.hk.commons.entity.product.AgreementToProduct;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.order.impl.ContractImpl;
import com.hk.service.product.IAgreementToProduct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Evan on 2017/7/6.
 */
@Service
public class AgreementToProductImpl extends BaseServiceImpl<AgreementToProduct> implements IAgreementToProduct {
    @Resource
    private JdbcTemplate jdbcTemplate;

    private Logger logger = LoggerFactory.getLogger(AgreementToProductImpl.class);

    RowMapper rowMapper = BeanPropertyRowMapper.newInstance(AgreementToProduct.class);

    @Override
    public List<AgreementToProduct> findByProductId(Long productId) {
        List<AgreementToProduct> result = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select a.id,a.cts,a.productId,a.agreementId,i.name as agreementTitle from agreement_to_product a LEFT JOIN t_agreement_info i on a.agreementId = i.id ");
        sql.append(" where a.productId= ?");
        try {
             result = jdbcTemplate.query(sql.toString(),rowMapper,productId);
        }catch (Exception e){
            logger.error("获取产品所属合同（含标题）异常！",e);
        }
        return result;
    }

    @Override
    public List<AgreementToProduct> getAgreementsByProductId(Long productId) {
        List<AgreementToProduct> result = null;
        String sql = "select * from agreement_to_product where productId = ?";
        try {
            result = jdbcTemplate.query(sql,rowMapper,productId);
        }catch (Exception e){
            logger.error("获取产品所属合同异常！",e);
        }
        return result;
    }

    @Override
    public List<Long> getAgreementIdsByProductId(Long productId) {
        List<Long> result = null;
        String sql = "select agreementId from agreement_to_product where productId =? ";
        try {
            result = jdbcTemplate.queryForList(sql,Long.class,productId);
        }catch (Exception e){
            logger.error("获取合同ID列表异常！productId={}",productId,e);
        }
        return result;
    }

    @Override
    public void updateOrSave(List<Long> agreementIds, Long productId) {
        String sqlDelete ="delete from  agreement_to_product where productId=?";
        try {
            //清掉原有数据
            jdbcTemplate.update(sqlDelete,productId);
            for(Long agreementId :agreementIds){
                AgreementToProduct atp = new AgreementToProduct();
                atp.setId(LongId.get());
                atp.setAgreementId(agreementId);
                atp.setProductId(productId);
                atp.setCts(System.currentTimeMillis());
                super.save(atp);
            }
        }catch (Exception e){
            logger.error("保存或更新产品关联合同异常！",e);
        }

    }
}
